package com.ay.weixin.fdc.boss.yongjin.db;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lizar.db.ibatis.Dao;

import org.lizar.config.keys.Keys;

import com.ay.weixin.fdc.boss.yongjin.domain.CashFlow;
import com.ay.weixin.fdc.boss.yongjin.domain.Withdraw;

public class IbatisCashFlow extends Dao {
	public void insert(CashFlow cash) throws SQLException{
		this.sqlMapper.insert("fdc_cash_flow.insert",cash);
	}
	
	public CashFlow get(long id) throws SQLException{
		return (CashFlow)this.sqlMapper.queryForObject("fdc_cash_flow.get",id);
	}
	
	public List<Withdraw> getWithdraw(long wxproject_id,int out_status) throws SQLException{
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("flow_out",Withdraw.FLOW_OUT);
		map.put("out_status",out_status);
		map.put("user",Keys.getStr("usercenter.user_table_name"));
		map.put("wxproject_id",wxproject_id);
		return (List<Withdraw>)this.sqlMapper.queryForList("fdc_cash_flow.getWithdraw",map);
	}
	
	
	
	public int countByOutStatus(long wxproject_id,int status) throws SQLException{
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("flow_out",Withdraw.FLOW_OUT);
		map.put("out_status",status);
		map.put("wxproject_id",wxproject_id);
		Integer num=(Integer)this.sqlMapper.queryForObject("fdc_cash_flow.countByOutStatus",map);
		if(num==null)return 0;
		return num;
	}
	
	public int countByOutStatusAndName(long wxproject_id,int status,String user_name) throws SQLException{
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("flow_out",Withdraw.FLOW_OUT);
		map.put("out_status",status);
		map.put("user_name",user_name);
		map.put("user",Keys.getStr("usercenter.user_table_name"));
		map.put("wxproject_id",wxproject_id);
		Integer num=(Integer)this.sqlMapper.queryForObject("fdc_cash_flow.countByOutStatusAndName",map);
		if(num==null)return 0;
		return num;
	}
	
	public List<Withdraw> getWithdrawByName(long wxproject_id,int out_status,String user_name) throws SQLException{
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("flow_out",Withdraw.FLOW_OUT);
		map.put("out_status",out_status);
		map.put("user_name",user_name);
		map.put("user",Keys.getStr("usercenter.user_table_name"));
		map.put("wxproject_id",wxproject_id);
		return (List<Withdraw>)this.sqlMapper.queryForList("fdc_cash_flow.getWithdrawByName",map);
	}
	
	
	public void update_approve(CashFlow flow) throws SQLException{
		this.sqlMapper.update("fdc_cash_flow.update_approve",flow);
	}
	
	public void update_in_status_cash_to_out(long user_id) throws SQLException{
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("user_id",user_id);
		map.put("start_in_status",CashFlow.IN_STATUS_APPLY_OUT);
		map.put("end_in_status",CashFlow.IN_STATUS_OUT);
		map.put("update_timestamp",System.currentTimeMillis());
		this.sqlMapper.update("fdc_cash_flow.update_in_status_cash_to_out",map);
	}
	
	public CashFlow getLastWithdraw(long user_id) throws SQLException{
		Map<String,Long> map=new HashMap<String,Long>();
		map.put("user_id",user_id);
		map.put("flow_in",Long.parseLong(Integer.toString(CashFlow.FLOW_OUT)));
		return (CashFlow)this.sqlMapper.queryForObject("fdc_cash_flow.getLastWithdraw",map);
	}
	public void markWithdraw(long user_id,long start_id,long end_id) throws SQLException{
		Map<String,Long> map=new HashMap<String,Long>();
		map.put("user_id",user_id);
		map.put("start_id",start_id);
		map.put("end_id",end_id);
		map.put("in_status",Long.parseLong(Integer.toString(CashFlow.IN_STATUS_IN)));
		map.put("flow_in",Long.parseLong(Integer.toString(CashFlow.FLOW_IN)));
		map.put("apply_status",Long.parseLong(Integer.toString(CashFlow.IN_STATUS_APPLY_OUT)));
		this.sqlMapper.update("fdc_cash_flow.markWithdraw",map);
	}
	
	public List<CashFlow> getSpecificFlow(long user_id,int flow_in,int in_status) throws SQLException{
		Map<String,Long> map=new HashMap<String,Long>();
		map.put("user_id",user_id);
		map.put("flow_in",Long.parseLong(Integer.toString(flow_in)));
		map.put("in_status",Long.parseLong(Integer.toString(in_status)));
		return (List<CashFlow>)this.sqlMapper.queryForList("fdc_cash_flow.getSpecificFlow",map);
	}
	
	public float countSpecificInStatusFlow(long user_id,int in_status) throws SQLException{
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("user_id",user_id);
		map.put("flow_in",CashFlow.FLOW_IN);
		map.put("in_status",Long.parseLong(Integer.toString(in_status)));
		Float obj=(Float)this.sqlMapper.queryForObject("fdc_cash_flow.countSpecificInStatusFlow",map);
		if(obj==null)return 0;
		return obj;
	}
	
	public List<CashFlow> getCurrentFlowIn(long user_id,int flow_in) throws SQLException{
		Map<String,Long> map=new HashMap<String,Long>();
		map.put("user_id",user_id);
		map.put("flow_in",Long.parseLong(Integer.toString(flow_in)));
		return (List<CashFlow>)this.sqlMapper.queryForList("fdc_cash_flow.getCurrentFlowIn",map);
	}
	

	
	public float getCashTotalFlowIn(long user_id) throws SQLException{
		Map<String,Long> map=new HashMap<String,Long>();
		map.put("user_id",user_id);
		map.put("flow_in",Long.parseLong(Integer.toString(CashFlow.FLOW_IN)));
		Float obj=(Float)this.sqlMapper.queryForObject("fdc_cash_flow.getCashTotalFlowIn",map);
		if(obj==null)return 0;
		return obj;
		
	}
	

	public float countCashAll(long user_id,int flow_in) throws SQLException{
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("user_id",user_id);
		map.put("flow_in",flow_in);
		Float obj=(Float)this.sqlMapper.queryForObject("fdc_cash_flow.countCashAll",map);
		if(obj==null)return 0;
		return obj;
	}
	
	public List<CashFlow> getTotalFlowFisrtPage(long user_id) throws SQLException{
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("user_id",Long.toString(user_id));
		map.put("size",Keys.getInt("usercenter.my_invite_list_first_page_size"));
		return (List<CashFlow>)this.sqlMapper.queryForList("fdc_cash_flow.getTotalFlowFisrtPage",map);
	}
	
	
	public List<CashFlow> getTotalFlowNextPage(long user_id,long from_id) throws SQLException{
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("user_id",Long.toString(user_id));
		map.put("id",Long.toString(from_id));
		map.put("size",Keys.getInt("usercenter.my_invite_list_first_page_size"));
		return (List<CashFlow>)this.sqlMapper.queryForList("fdc_cash_flow.getTotalFlowNextPage",map);
	}
	
}
